﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using VanTaiBLL;
using VanTaiData;
using VanTaiProperty;
using Roles;
using System.Web.UI.HtmlControls;

namespace VanTaiTMS.Page
{
    public partial class danh_muc_xe : System.Web.UI.Page
    {
        #region[Define]
        private DataTable dtVehicle;
        private DataTable dtVehicleDetail_Edit;
        private DataTable dtTypeVehicle;
        private DataTable dtMember;
        private DataTable dtGroupVehicle;
        private DataTable dtSearch;
        protected string SuggestionList;
        protected string StrAlert = "";
        private DataTable dtAutoComplete;
        private static bool flag = false;
        private clsTMType_VehicleBusiness clsTypeVehicle = new clsTMType_VehicleBusiness();
        private clsTMVehicleBusiness clsVehicle = new clsTMVehicleBusiness();
        private clsTMMemberBusiness clsMember = new clsTMMemberBusiness();
        private clsTMVehicleGroupBusiness clsGroupVehicle = new clsTMVehicleGroupBusiness();
        #endregion

        #region[Controller]
        protected void Page_Load(object sender, EventArgs e)
        {
            CheckPermisstion();
            Autocomplate();
            if (!IsPostBack)
            {
                BindGridVehicle();
                BindGridTypeVehicle();
                BindGridMember();
                BindGridVehicleGroup();
            }
        }
        protected void cmdAddNew_Click(object sender, EventArgs e)
        {
            flag = true;
            lbThongBaoLoi.Text = "";
            txtSoXe.Enabled = true;
            txtNgayDangKiem.Text = DateTime.Now.ToString();
            txtNgayHetHang.Text = Convert.ToDateTime(DateTime.Now.AddDays(60.0)).ToString("dd/MM/yyyy");
            ClearTextBox();
            ClearDropdownlist();
            OpenDialog("#danhmucxe");
            imgSave.ToolTip = "Thêm mới";
            upNewUpdatePanel.Update();
            imgSave.Visible = true;
        }
        protected void cmdEdit_Click(object sender, EventArgs e)
        {
            lbThongBaoLoi.Text = "";
            txtSoXe.Enabled = false;
            for (int i = 0; i < ListViewAll.Items.Count; i++)
            {
                CheckBox chkView = (CheckBox)ListViewAll.Items[i].FindControl("chkList");
                Label lbTrangThai = (Label)ListViewAll.Items[i].FindControl("lbDuyet");
                if (chkView != null)
                {
                    if (chkView.Checked)
                    {
                        BindGridVehicleDetail_Edit(Convert.ToInt32(ListViewAll.DataKeys[i].Value.ToString()));
                        OpenDialog("#danhmucxe");
                        upNewUpdatePanel.Update();
                        imgSave.ToolTip = "Cập nhật";
                        flag = false;
                    }
                }
            }
        }
        protected void cmdDel_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < ListViewAll.Items.Count; i++)
            {
                CheckBox chkHide = (CheckBox)ListViewAll.Items[i].FindControl("chkList");
                Label lbTrangThai = (Label)ListViewAll.Items[i].FindControl("lbDuyet");
                if (chkHide != null)
                {
                    if (chkHide.Checked)
                    {
                        try
                        {
                            if (clsVehicle.TMVehicle_Check_Delete_TMCommandTransferOrder(int.Parse(ListViewAll.DataKeys[i].Value.ToString())) == false
                                && clsVehicle.TMVehicle_Check_Delete_TMDiaryTransferOrder(int.Parse(ListViewAll.DataKeys[i].Value.ToString())) == false
                                && clsVehicle.TMVehicle_Check_Delete_TMPlan_TransferOrder(int.Parse(ListViewAll.DataKeys[i].Value.ToString())) == false
                                && clsVehicle.TMVehicle_Check_Delete_TMSalesReport(int.Parse(ListViewAll.DataKeys[i].Value.ToString())) == false
                                && clsVehicle.TMVehicle_Check_Contract(int.Parse(ListViewAll.DataKeys[i].Value.ToString())) == false
                                )
                            {
                                VanTaiBLL.clsTMVehicleBusiness.DeleteTMVehicle(int.Parse(ListViewAll.DataKeys[i].Value.ToString()));
                            }
                            else
                            {
                                lbThongBaoXoa.Text = "Mẫu tin này vẩn còn tồn tại.Không thể xoá !";
                                OpenDialog("#thongbaoxoa");
                                upDatePanelThongBaoXoa.Update();
                                cmdAgree.Visible = false;
                                cmdClose.Text = "Đóng lại";
                            }
                        }
                        catch (Exception ex)
                        {
                            lbThongBaoXoa.Text = ex.ToString();
                            OpenDialog("#thongbaoxoa");
                            upDatePanelThongBaoXoa.Update();
                            cmdAgree.Visible = false;
                            cmdClose.Text = "Đóng lại";
                        }
                    }
                }
            }
        }
        protected void cmdSerach_Click(object sender, EventArgs e)
        {
            if (txtSearch.Text == "")
            {
                StrAlert += "<div class='alert alert-warning'>";
                StrAlert += "<a href='#' class='close' data-dismiss='alert'>&times;</a>";
                StrAlert += "Vui lòng nhập vào từ khoá để tìm kiếm !";
                StrAlert += "</div>";
            }
            else
            {
                Search(txtSearch.Text);
            }
        }
        protected void cmdRefesh_Click(object sender, EventArgs e)
        {
            txtSearch.Text = "";
            BindGridTypeVehicle();
        }
        protected void cmdExport_Click(object sender, EventArgs e)
        {
            OpenDialog("#exporttoexcel");
        }
        protected void cmdExportToExcel_Click(object sender, EventArgs e)
        {
        }
        protected void btnSetNgayConLai_Click(object sender, EventArgs e)
        {
            int highlight = int.Parse(txtSetNgay.Text);
            System.IO.StreamWriter writer = new System.IO.StreamWriter(Server.MapPath("~/NgayConLai.txt"));
            writer.WriteLine(highlight);
            writer.Close();
        }
        //---------------button-popup---------------------
        protected void imgrefesh_Click(object sender, ImageClickEventArgs e)
        {
            RefeshDropdownlist();
        }
        protected void imgSave_Click(object sender, ImageClickEventArgs e)
        {
            if (txtSoXe.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền vào số xe !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
            if (txtNgayDangKiem.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền chọn ngày đăng kiếm !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
            if (drLoaiXe.SelectedValue.ToString() == "-1")
            {
                lbThongBaoLoi.Text = "Vui lòng chọn loại xe !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
            if (drNhomXe.SelectedValue.ToString() == "-1")
            {
                lbThongBaoLoi.Text = "Vui lòng chọn nhóm xe !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
            if (drTaiXe.SelectedValue.ToString() == "-1")
            {
                lbThongBaoLoi.Text = "Vui lòng chọn tài xế !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
            if (txtNgayHetHang.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng chọn ngày hết hạn !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
            if (txtSoXe.Text != "" && txtNgayDangKiem.Text != "" && txtNgayHetHang.Text != "" && drLoaiXe.SelectedValue.ToString() != "-1" && drNhomXe.SelectedValue.ToString() != "-1" && drTaiXe.SelectedValue.ToString() != "-1")
            {
                Insert();
            }
        }
        protected void cmdAgree_Click(object sender, EventArgs e)
        {
            
        }
        protected void cmdClose_Click(object sender, EventArgs e)
        {
            CloseDialog("#thongbaoxoa");
        }
        protected void cmdCloseThongBao_Click(object sender, EventArgs e)
        {
            CloseDialog("#thongbao");
        }
        protected void ImgRefeshDuyet_Click(object sender, ImageClickEventArgs e)
        {

        }
        protected void ImgRefeshExport_Click(object sender, ImageClickEventArgs e)
        {
            //txtTuNgayNhapKhoExport.Text = "";
            //txtDenNgayNhapKhoExport.Text = "";
            //BindGridLenhDieuDongExportExcel();
        }
        //-------------list-view-----------------------
        protected void ListViewAll_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            try
            {
                HttpCookie cookie = Request.Cookies["UserLogin"];
                bool Edit = SecurityServices.HasPermisstion(StringContent.QuanLyDanhMucXe, int.Parse(cookie["RolesID"]), TypeaAudit.Edit);
                lbThongBaoLoi.Text = "";
                txtSoXe.Enabled = false;
                if (e.CommandName == "Accept")
                {
                    if (Edit == false)
                    {
                        imgSave.Visible = false;
                        string SenderId = ((HtmlInputHidden)e.Item.FindControl("hiddenId")).Value;
                        BindGridVehicleDetail_Edit(int.Parse(SenderId));
                        OpenDialog("#danhmucxe");
                        upNewUpdatePanel.Update();
                        imgSave.ToolTip = "Cập nhật";
                        flag = false;
                    }
                    else
                    {
                        imgSave.Visible = true;
                        string SenderId = ((HtmlInputHidden)e.Item.FindControl("hiddenId")).Value;
                        BindGridVehicleDetail_Edit(int.Parse(SenderId));
                        OpenDialog("#danhmucxe");
                        upNewUpdatePanel.Update();
                        imgSave.ToolTip = "Cập nhật";
                        flag = false;
                    }
                }
            }
            catch (Exception ex)
            {
                lbThongBaoLoi.Text = ex.ToString();
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
        }
        protected void ListViewAll_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                LinkButton linkListName = (LinkButton)e.Item.FindControl("linkListName");
                string _jsDouble1 = ClientScript.GetPostBackClientHyperlink(linkListName, "");


                linkListName.Attributes["ondblclick"] = _jsDouble1;
                linkListName.Attributes["onclick"] = "return false;";

                //--------------------------High Light-------------------------------
                ListViewDataItem dataitem = (ListViewDataItem)e.Item;
                DateTime date1 = Convert.ToDateTime(DataBinder.Eval(dataitem.DataItem, "Date_Of_Expires"));
                DateTime date2 = Convert.ToDateTime(DateTime.Today);
                TimeSpan timespan = date1.Subtract(date2);
                int intDays = timespan.Days;
                if (intDays < SetNgayConLaiDangKiemXe() && intDays >= 0)
                {
                    HtmlTableRow cell = (HtmlTableRow)e.Item.FindControl("HightLightRow");
                    cell.BgColor = "Red";
                    cell.Attributes["class"] = "rowError";
                }
                else if (intDays < 0)
                {
                    HtmlTableRow cell = (HtmlTableRow)e.Item.FindControl("HightLightRow");
                    cell.BgColor = "Black";
                }
            }
        }
        //-----------pagger-----------------------
        protected void DataPagerListAll_PreRender(object sender, EventArgs e)
        {
            DataPagerListAll.PageSize = 17;
            BindGridVehicle();
        }
        //---------Check-all--------------
        protected void chkCheckAll_CheckedChanged(object sender, EventArgs e)
        {

        }
        //---------Dropdownlist-----------
        protected void drTrongTai_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }
        //----------Thêm-nhanh------------
        protected void linkThemNhanhLoaiXe_Click(object sender, EventArgs e)
        {
            OpenDialog("#themnhanh");
        }
        protected void linkThemNhanhNhomXe_Click(object sender, EventArgs e)
        {
            OpenDialog("#themnhanhnhomxe");
        }
        protected void linkThemNhanhTaiXe_Click(object sender, EventArgs e)
        {
            OpenDialog("#themnhanhtaixe");
        }
        #endregion

        #region[Function]
        protected void BindGridVehicle()
        {
            dtVehicle = clsVehicle.Search(txtSearch.Text);
            if (dtVehicle != null && dtVehicle.Rows.Count > 0)
            {
                ListViewAll.DataSource = dtVehicle;
                ListViewAll.DataBind();
                ListViewAll.Visible = true;
                DataPagerListAll.Visible = true;
            }
            else
            {
                ListViewAll.Visible = false;
                DataPagerListAll.Visible = false;
            }
        }
        protected void BindGridVehicleDetail_Edit(int id)
        {
            dtVehicleDetail_Edit = clsVehicle.BindGirdTMVehicle_Detail_Edit(id);
            if (dtVehicleDetail_Edit != null && dtVehicleDetail_Edit.Rows.Count > 0)
            {
                lbID.Text = dtVehicleDetail_Edit.Rows[0]["Vehicle_ID"].ToString();
                lbIDXoa.Text = dtVehicleDetail_Edit.Rows[0]["Vehicle_ID"].ToString();
                txtSoXe.Text = dtVehicleDetail_Edit.Rows[0]["Vehicle_Number"].ToString();
                if (dtVehicleDetail_Edit.Rows[0]["Vehicle_Type_Vehicle_ID"] != DBNull.Value)
                {
                    drLoaiXe.SelectedValue = dtVehicleDetail_Edit.Rows[0]["Vehicle_Type_Vehicle_ID"].ToString();
                }
                if (dtVehicleDetail_Edit.Rows[0]["VehicleGroup_Old_ID"] !=DBNull.Value)
                {
                    drNhomXe.SelectedValue = dtVehicleDetail_Edit.Rows[0]["VehicleGroup_Old_ID"].ToString();
                }
                if (dtVehicleDetail_Edit.Rows[0]["Member_ID"] != DBNull.Value)
                {
                    drTaiXe.SelectedValue = dtVehicleDetail_Edit.Rows[0]["Member_ID"].ToString();
                }
                txtNgayDangKiem.Text = String.Format("{0:dd/MM/yyyy}", dtVehicleDetail_Edit.Rows[0]["Date_Of_Reg"]).ToString();
                txtNgayHetHang.Text = String.Format("{0:dd/MM/yyyy}", dtVehicleDetail_Edit.Rows[0]["Date_Of_Expires"]).ToString();
                txtGhiChu.Text = dtVehicleDetail_Edit.Rows[0]["Note_Vehicle"].ToString();
            }
        }
        protected void BindGridTypeVehicle()
        {
            dtTypeVehicle = clsTypeVehicle.BindGirdTMType_Vehicle();
            if (dtTypeVehicle != null && dtTypeVehicle.Rows.Count > 0)
            {
                drLoaiXe.DataSource = dtTypeVehicle;
                drLoaiXe.DataTextField = "Type_Vehicle_Name";
                drLoaiXe.DataValueField = "Type_Vehicle_ID";
                drLoaiXe.DataBind();
            }
        }
        protected void BindGridMember()
        {
            dtMember = clsMember.BindGirdTMMember();
            if (dtMember != null && dtMember.Rows.Count > 0)
            {
                drTaiXe.DataSource = dtMember;
                drTaiXe.DataTextField = "FullNameMem";
                drTaiXe.DataValueField = "Member_ID";
                drTaiXe.DataBind();
            }
        }
        protected void BindGridVehicleGroup()
        {
            dtGroupVehicle = clsGroupVehicle.BindGirdTMVehicleGroup();
            if (dtGroupVehicle != null && dtGroupVehicle.Rows.Count > 0)
            {
                drNhomXe.DataSource = dtGroupVehicle;
                drNhomXe.DataTextField = "VehicleGroup_Name";
                drNhomXe.DataValueField = "VehicleGroup_ID";
                drNhomXe.DataBind();
            }
        }
        protected void Insert()
        {
            //Insert
            if (flag)
            {
                InsertVehicle();
            }
            else//Update
            {
                UpdateVehicle();
            }
        }
        protected bool CheckInsert()
        {
            bool kiemtra = false;
            dtVehicle = clsVehicle.BindGirdTMVehicle();
            if (dtVehicle != null && dtVehicle.Rows.Count > 0)
            {
                foreach (DataRow dr in dtVehicle.Rows)
                {
                    if (txtSoXe.Text == dr["Vehicle_Number"].ToString())
                    {
                        kiemtra = true;
                    }
                }
            }
            return kiemtra;
        }
        protected void InsertVehicle()
        {
            try
            {
                if (CheckInsert())
                {
                    lbThongBaoLoi.Text = "Số xe này đã tồn tại !";
                    OpenDialog("#thongbao");
                    updatePanelThongBao.Update();
                }
                else
                {
                    VanTaiProperty.clsTMVehicleProperty obj = new clsTMVehicleProperty();
                    obj.Vehicle_Number = txtSoXe.Text;
                    obj.Vehicle_Type_Vehicle_ID = int.Parse(drLoaiXe.SelectedValue.ToString());
                    obj.VehicleGroup_Old_ID = int.Parse(drNhomXe.SelectedValue.ToString());
                    obj.Member_ID = int.Parse(drTaiXe.SelectedValue.ToString());
                    obj.Date_Of_Reg = DateTime.Parse(txtNgayDangKiem.Text);
                    obj.Date_Of_Expires = DateTime.Parse(txtNgayHetHang.Text);
                    obj.Note_Vehicle = txtGhiChu.Text;
                    //---------------------
                    obj.Division_Old_ID = 0;
                    obj.Division_New_ID = 0;
                    obj.VehicleGroup_New_ID = 0;
                    obj.Decision_ID = 0;
                    obj.Create_Date_Vehicle = DateTime.Parse(txtNgayDangKiem.Text);
                    obj.Active_Vehicle = false;
                    obj.HMCT_ID = 0;
                    obj.MaCha = DBNull.Value.ToString();

                    VanTaiBLL.clsTMVehicleBusiness.InsertTMVehicle(obj);
                    lbThongBaoLoi.Text = "Thêm mẫu tin thành công !";
                    OpenDialog("#thongbao");
                    updatePanelThongBao.Update();
                    BindGridVehicle();
                    ClearTextBox();
                    ClearDropdownlist();
                }
            }
            catch (Exception ex)
            {
                lbThongBaoLoi.Text = ex.ToString();
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
        }
        protected void UpdateVehicle()
        {
            try
            {
                VanTaiProperty.clsTMVehicleProperty obj = new clsTMVehicleProperty();
                obj.Vehicle_ID = int.Parse(lbID.Text);
                obj.Vehicle_Number = txtSoXe.Text;
                obj.Vehicle_Type_Vehicle_ID = int.Parse(drLoaiXe.SelectedValue.ToString());
                obj.VehicleGroup_Old_ID = int.Parse(drNhomXe.SelectedValue.ToString());
                obj.Member_ID = int.Parse(drTaiXe.SelectedValue.ToString());
                obj.Date_Of_Reg = DateTime.Parse(txtNgayDangKiem.Text);
                obj.Date_Of_Expires = DateTime.Parse(txtNgayHetHang.Text);
                obj.Note_Vehicle = txtGhiChu.Text;
                //---------------------
                obj.Division_Old_ID = 0;
                obj.Division_New_ID = 0;
                obj.VehicleGroup_New_ID = 0;
                obj.Decision_ID = 0;
                obj.Create_Date_Vehicle = DateTime.Parse(txtNgayDangKiem.Text);
                obj.Active_Vehicle = false;
                obj.HMCT_ID = 0;
                obj.MaCha = DBNull.Value.ToString();

                VanTaiBLL.clsTMVehicleBusiness.UpdateTMVehicle(obj);
                lbThongBaoLoi.Text = "Cập nhật mẫu tin thành công !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
                BindGridTypeVehicle();
            }
            catch (Exception ex)
            {
                lbThongBaoLoi.Text = ex.ToString();
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
        }
        //Set Ngay Con Lai Dang Kiem Xe De Hien Thi Mau Canh Bao
        protected int SetNgayConLaiDangKiemXe()
        {
            int highlight;
            if (System.IO.File.Exists(Server.MapPath("~/NgayConLai.txt")) == false)
            {
                highlight = 30;
            }
            else
            {
                var read = new System.IO.StreamReader(Server.MapPath("~/NgayConLai.txt"));
                highlight = int.Parse(read.ReadLine());
                read.Close();
            }
            return highlight;
        }

        //AutoComplate
        protected void Autocomplate()
        {
            dtAutoComplete = clsVehicle.BindGirdTMVehicle();
            if (dtAutoComplete != null && dtAutoComplete.Rows.Count > 0)
            {
                foreach (DataRow dr in dtAutoComplete.Rows)
                {
                    if (string.IsNullOrEmpty(SuggestionList))
                    {
                        SuggestionList += "\"" + dr["Vehicle_Number"].ToString().Replace("\n", " ") + "\",";
                        SuggestionList += "\"" + dr["VehicleGroup_Name"].ToString().Replace("\n", " ") + "\",";
                        SuggestionList += "\"" + dr["M_FullNameMem"].ToString().Replace("\n", " ") + "\",";
                    }
                    else
                    {
                        SuggestionList += "\"" + dr["Vehicle_Number"].ToString().Replace("\n", " ") + "\",";
                        SuggestionList += "\"" + dr["VehicleGroup_Name"].ToString().Replace("\n", " ") + "\",";
                        SuggestionList += "\"" + dr["M_FullNameMem"].ToString().Replace("\n", " ") + "\",";
                    }
                }
            }
        }
        protected void Search(string keywords)
        {
            dtSearch = clsVehicle.Search(keywords);
            if (dtSearch != null && dtSearch.Rows.Count > 0)
            {
                ListViewAll.DataSource = dtSearch;
                ListViewAll.DataBind();
                ListViewAll.Visible = true;
                DataPagerListAll.Visible = true;

                StrAlert += "<div class='alert alert-success'>";
                StrAlert += "<a href='#' class='close' data-dismiss='alert'>&times;</a>";
                StrAlert += "Tìm thấy " + dtSearch.Rows.Count.ToString() + " mẫu tin !";
                StrAlert += "</div>";
            }
            else
            {
                ListViewAll.Visible = false;
                DataPagerListAll.Visible = false;
                StrAlert += "<div class='alert alert-error'>";
                StrAlert += "<a href='#' class='close' data-dismiss='alert'>&times;</a>";
                StrAlert += "Không tìm thấy mẫu tin nào !";
                StrAlert += "</div>";
            }
        }
        //Total
        #endregion

        #region[OpenDialog]
        private void OpenDialog(string dialogId)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('open');");
            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
               "scriptKey", sb.ToString(), true);
        }
        private void CloseDialog(string dialogId)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('close');");
            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
               "scriptKey", sb.ToString(), true);
        }
        #endregion

        #region[Clear-TextBox]
        protected void ClearTextBox()
        {
            txtSoXe.Text = "";
            txtNgayDangKiem.Text = DateTime.Now.ToString();
            txtNgayHetHang.Text = Convert.ToDateTime(DateTime.Now.AddDays(60.0)).ToString("dd/MM/yyyy");
        }
        protected void ClearDropdownlist()
        {
            drLoaiXe.SelectedIndex = -1;
            drNhomXe.SelectedIndex = -1;
            drTaiXe.SelectedIndex = -1;
        }
        protected void RefeshDropdownlist()
        {
            drLoaiXe.Items.Clear();
            drLoaiXe.Items.Add(new ListItem("--Vui lòng chọn loại xe--", "-1"));
            BindGridTypeVehicle();

            drTaiXe.Items.Clear();
            drTaiXe.Items.Add(new ListItem("--Vui lòng chọn tài xế--", "-1"));
            BindGridMember();

            drNhomXe.Items.Clear();
            drNhomXe.Items.Add(new ListItem("--Vui lòng chọn nhóm xe--", "-1"));
            BindGridVehicleGroup();
        }
        #endregion

        #region[Permisstion]
        protected void CheckPermisstion()
        {
            HttpCookie cookie = Request.Cookies["UserLogin"];
            if (cookie != null)
            {
                bool Is_Page_All = SecurityServices.HasPermisstionPage(StringContent.QuanLyDanhMucXe, int.Parse(cookie["RolesID"]), true);
                if (Is_Page_All == true)
                {
                    bool AddNew = SecurityServices.HasPermisstion(StringContent.QuanLyDanhMucXe, int.Parse(cookie["RolesID"]), TypeaAudit.AddNew);
                    if (AddNew == false)
                    {
                        cmdAddNew.Visible = false;
                    }
                    else
                    {
                        cmdAddNew.Visible = true;
                    }
                    bool Edit = SecurityServices.HasPermisstion(StringContent.QuanLyDanhMucXe, int.Parse(cookie["RolesID"]), TypeaAudit.Edit);
                    if (Edit == false)
                    {
                        cmdEdit.Visible = false;
                    }
                    else
                    {
                        cmdEdit.Visible = true;
                    }
                    bool Del = SecurityServices.HasPermisstion(StringContent.QuanLyDanhMucXe, int.Parse(cookie["RolesID"]), TypeaAudit.Delete);
                    if (Del == false)
                    {
                        cmdDel.Visible = false;
                    }
                    else
                    {
                        cmdDel.Visible = true;
                    }
                }
                else
                {
                    Response.Redirect("error-page.aspx");
                }
            }
        }
        #endregion
    }
}